//
//  SelectedIndustry.m
//  HappyStore
//
//  Created by 李小辉 on 16/11/29.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "SelectedIndustry.h"

@interface SelectedIndustry ()
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) NSArray *ary1;
@property (nonatomic, strong) NSArray *ary2;
@property (nonatomic, strong) NSArray *ary3;
@property (nonatomic, strong) NSString *numStr;
@property (nonatomic, strong) NSString *selectdeAry1Str;
@end



@implementation SelectedIndustry


+ (SelectedIndustry *)sharedSelectedIndustry{
    static SelectedIndustry *sharedService;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedService = [[SelectedIndustry alloc] initWithFrame:[UIScreen mainScreen].bounds];
        sharedService.backgroundColor = [[UIColor colorWithWhite:0.6 alpha:1.0f] colorWithAlphaComponent:0.7];
    });
    return sharedService;
}

- (void)btnClickNumStr:(NSString *)numStr{
    self.numStr = numStr;
    self.hidden = NO;
    //加入手势来取消键盘
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(dismissView)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapRecognizer];
    
    if ([numStr integerValue] == 1) {
        self.dataDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"industry" ofType:@"plist"]];
        self.ary1 = [self.dataDic allKeys];
        self.ary2 = [self.dataDic[self.ary1[0]] allKeys];
        self.ary3 = self.dataDic[self.ary1[0]][self.ary2[0]] ;
 
    }else{//选择单位

        self.dataDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"unit" ofType:@"plist"]];
        self.ary1 = [self.dataDic allKeys];
        self.ary2 = self.dataDic[self.ary1[0]];
    }
    
    
    self.picker = [[UIPickerView alloc] initWithFrame: CGRectMake(0, [UIScreen mainScreen].bounds.size.height-240 - 50, 320, 240)];
    self.picker.dataSource = self;
    self.picker.delegate = self;
    self.picker.showsSelectionIndicator = YES;
    [self.picker selectRow:0 inComponent:0 animated:YES];
    self.picker.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.picker];
    UIButton * buttonOK = [UIButton buttonWithType: UIButtonTypeCustom];
    [buttonOK setTitle: @"确  定" forState: UIControlStateNormal];

    [buttonOK setFrame: CGRectMake(0, self.picker.frame.origin.y + self.picker.frame.size.height, screenW, 50)];
    [buttonOK setTintColor: [UIColor grayColor]];

    [buttonOK setBackgroundColor:[UIColor colorWithRed:32/255.0f green:165/255.0f  blue:240/255.0f alpha:1.0f]];
    [buttonOK addTarget: self action: @selector(buttobClicked:) forControlEvents: UIControlEventTouchUpInside];

    buttonOK.titleLabel.font = [UIFont systemFontOfSize:20.0f];
    [self addSubview: buttonOK];
    //加到最上层
    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:self];
}
#pragma mark- Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if ([self.numStr integerValue] == 1) {
        return 3;
    }else{
        return 2;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if ([self.numStr integerValue] == 1) {
        if (component == 0) {
            return self.ary1.count;
        }else if (component ==1){
            return self.ary2.count;
        }else{
            return self.ary3.count;
        }
    }else{
        if (component == 0) {
            return self.ary1.count;
        }else{
            return self.ary2.count;
        }
    }
}
#pragma mark- Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if ([self.numStr integerValue] == 1) {
    
    if (component == 0) {
        return [self.ary1 objectAtIndex: row];
    }
    else if (component == 1) {
        return [self.ary2 objectAtIndex: row];
    }
    else {
        return [self.ary3 objectAtIndex: row];
    }
        
    }else{
        if (component == 0) {
            return self.ary1[row];
        }else{
            return self.ary2[row];
        }
    }
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([self.numStr integerValue] == 1) {
        if (component == 0) {
            self.selectdeAry1Str = self.ary1[row];
            self.ary2 = [self.dataDic[self.selectdeAry1Str] allKeys];
            self.ary3 = self.dataDic[self.selectdeAry1Str][self.ary2[0]];
            [self.picker selectRow:0 inComponent:1 animated:YES];
            [self.picker reloadComponent:1];
            [self.picker selectRow:0 inComponent:2 animated:YES];
            [self.picker reloadComponent:2];
        }else if (component == 1){
            self.ary3 = self.dataDic[self.selectdeAry1Str][self.ary2[row]];
            [self.picker selectRow:0 inComponent:2 animated:YES];
            [self.picker reloadComponent:2];
        }
    }else{
        
        if (component == 0) {
            self.ary2 = self.dataDic[self.ary1[row]];
            [self.picker selectRow:0 inComponent:1 animated:YES];
            [self.picker reloadComponent:1];
        }
    }
    
    
}


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if ([self.numStr integerValue] == 1) {
        
        if (component == 0) {
            return 80;
        }
        else if (component == 1) {
            return 100;
        }
        else {
            return 115;
        }
        
    }else{
        return 150;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    if ([self.numStr integerValue] == 1) {
        if (component == 0) {
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 78, 30)];
            myView.textAlignment = NSTextAlignmentCenter;
            myView.text = [self.ary1 objectAtIndex:row];
            myView.font = [UIFont systemFontOfSize:14];
            myView.backgroundColor = [UIColor clearColor];
        }
        else if (component == 1) {
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 95, 30)];
            myView.textAlignment = NSTextAlignmentCenter;
            myView.text = [self.ary2 objectAtIndex:row];
            myView.font = [UIFont systemFontOfSize:14];
            myView.backgroundColor = [UIColor clearColor];
        }
        else {
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 110, 30)];
            myView.textAlignment = NSTextAlignmentCenter;
            myView.text = [self.ary3 objectAtIndex:row];
            myView.font = [UIFont systemFontOfSize:14];
            myView.backgroundColor = [UIColor clearColor];
        }
    }else{
        if (component == 0) {
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150, 30)];
            myView.textAlignment = NSTextAlignmentCenter;
            myView.text = [self.ary1 objectAtIndex:row];
            myView.font = [UIFont systemFontOfSize:14];
            myView.backgroundColor = [UIColor clearColor];
        }else {
            myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150, 30)];
            myView.textAlignment = NSTextAlignmentCenter;
            myView.text = [self.ary2 objectAtIndex:row];
            myView.font = [UIFont systemFontOfSize:14];
            myView.backgroundColor = [UIColor clearColor];
        }
    }
    
    
    return myView;
}
- (void)dismissView
{
    self.hidden = YES;
}

- (void)buttobClicked:(UIButton *)sender{
    NSString *industryStr = @"";
    if ([self.numStr integerValue] == 1) {
        NSInteger districtIndex1 = [self.picker selectedRowInComponent: 2];
        NSInteger districtIndex2 = [self.picker selectedRowInComponent: 1];
        NSInteger districtIndex3 = [self.picker selectedRowInComponent:0];
        NSString *str1 = self.ary1[districtIndex3];
        NSString *str2 = self.ary2[districtIndex2];
        NSString *str3 = self.ary3[districtIndex1];
        if ([str1 isEqualToString:str2]) {
            industryStr = [NSString stringWithFormat:@"%@-%@",str2,str3];
        }else if ([str2 isEqualToString:str3]) {
            industryStr = [NSString stringWithFormat:@"%@-%@",str1,str2];
        }else{
            industryStr = [NSString stringWithFormat:@"%@-%@-%@",str1,str2,str3];
        }
    }else{
        NSInteger districtIndex2 = [self.picker selectedRowInComponent: 1];
        industryStr = self.ary2[districtIndex2];
    }
    [self.delegate selectedIndustry:industryStr];
}

@end
